<html>
<head><meta charset="utf-8"><title>Internal &quot;self&quot; references into pinned types and Atomic · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Internal.20.22self.22.20references.20into.20pinned.20types.20and.20Atomic.html">Internal &quot;self&quot; references into pinned types and Atomic</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212923812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Internal%20%22self%22%20references%20into%20pinned%20types%20and%20Atomic/near/212923812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Internal.20.22self.22.20references.20into.20pinned.20types.20and.20Atomic.html#212923812">(Oct 10 2020 at 16:44)</a>:</h4>
<p>If you have a pinned type (e.g. <code>Pin&lt;&amp;mut Foo&gt;</code>) which has a atomic field like <code>static Foo { ...., atomic: AtomicUsize, ... }</code> and we have some references to the atomic which relies on the guarantees of <code>Pin</code> but might e.g. be from a separate thread. </p>
<p>Then do we need to have a <code>UnsafeCell&lt;AtomicUsize&gt;</code> (or similar)?</p>
<p>The thing is we basically have (and use concurrently&amp;parallel) a pointer to the  atomic but at the same time through the <code>Pin&lt;&amp;mut ...&gt;</code> we do have a <code>&amp;mut</code> reference to the field.</p>
<p>Furthermore the field is on stack, so maybe the compiler have  some assumptions about aliasing rules.</p>
<p>And <code>Atomic*</code> types have <code>&amp;mut self</code> method relying on it then only being accessed by that reference.</p>
<p>My guess is as long as we don't access the atomic field through a <code>&amp;mut</code> (even if we access the struct through a <code>&amp;mut</code>) it would be fine. But I'm really unsure about what the compiler and especially llvm might do because of the aliasing guarantees we give out with <code>&amp;mut</code> (through we might currently not pass them to llvm?)??</p>



<a name="212923973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Internal%20%22self%22%20references%20into%20pinned%20types%20and%20Atomic/near/212923973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Internal.20.22self.22.20references.20into.20pinned.20types.20and.20Atomic.html#212923973">(Oct 10 2020 at 16:49)</a>:</h4>
<p><code>AtomicUsize</code> is a wrapper around <code>UnsafeCell&lt;usize&gt;</code>.</p>



<a name="212924017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Internal%20%22self%22%20references%20into%20pinned%20types%20and%20Atomic/near/212924017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Internal.20.22self.22.20references.20into.20pinned.20types.20and.20Atomic.html#212924017">(Oct 10 2020 at 16:50)</a>:</h4>
<p>All relevant <code>AtomicUsize</code> methods should take <code>&amp;self</code>.</p>



<a name="212924097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Internal%20%22self%22%20references%20into%20pinned%20types%20and%20Atomic/near/212924097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Internal.20.22self.22.20references.20into.20pinned.20types.20and.20Atomic.html#212924097">(Oct 10 2020 at 16:53)</a>:</h4>
<p>You must never have two <code>&amp;mut</code> pointing to the same location at the same time. While it is currently not used, when <code>-Zmutable-noalias=yes</code> is used, LLVM will be told that two <code>&amp;mut</code> don't alias. It is currently disabled because there used to be a bug in LLVM. I don't know if it is already fixed.</p>



<a name="212924473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Internal%20%22self%22%20references%20into%20pinned%20types%20and%20Atomic/near/212924473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Internal.20.22self.22.20references.20into.20pinned.20types.20and.20Atomic.html#212924473">(Oct 10 2020 at 17:01)</a>:</h4>
<p>Thanks a lot.</p>
<p>Given that's a <code>UnsafeCell&lt;usize&gt;</code> internally I know it <del>will</del> should work.</p>
<p>For clarification, Does having a <code>&amp;mut Foo</code> (which you don't use to access the <code>bar</code> field and a ~<code>&amp;mut Foo.bar</code>  counting as different memory locations or does the <code>&amp;mut Foo</code> implies a <code>&amp;mut Foo.bar</code>?</p>
<p>(The <code>&amp;mut self</code> method I meant is: <code>pub fn get_mut(&amp;mut self) -&gt; &amp;mut usize</code>, but this no longer matters for my question now that I know that it uses <code>UnsafeCell</code> internally).</p>



<a name="212924610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Internal%20%22self%22%20references%20into%20pinned%20types%20and%20Atomic/near/212924610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Internal.20.22self.22.20references.20into.20pinned.20types.20and.20Atomic.html#212924610">(Oct 10 2020 at 17:02)</a>:</h4>
<blockquote>
<p>or does the <code>&amp;mut Foo</code> implies a <code>&amp;mut Foo.bar</code>?</p>
</blockquote>
<p>Yes</p>



<a name="212925398"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Internal%20%22self%22%20references%20into%20pinned%20types%20and%20Atomic/near/212925398" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Internal.20.22self.22.20references.20into.20pinned.20types.20and.20Atomic.html#212925398">(Oct 10 2020 at 17:09)</a>:</h4>
<p>Thanks a lot knowing this helps quite a bit. <span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span></p>



<a name="212925680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Internal%20%22self%22%20references%20into%20pinned%20types%20and%20Atomic/near/212925680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Internal.20.22self.22.20references.20into.20pinned.20types.20and.20Atomic.html#212925680">(Oct 10 2020 at 17:14)</a>:</h4>
<p>Wait, quoting rust doc:</p>
<blockquote>
<p>There is no legal way to obtain aliasing &amp;mut, not even with UnsafeCell&lt;T&gt;</p>
</blockquote>
<p>But doesn't that men futures are <del>broken</del> (edit, more like tricky) as they do have both <code>&amp;mut</code> to the future and potential other <code>&amp;mut</code>/<code>&amp;</code> in the internally borrows? It probably is not a problem for async only. But the moment you pass a reference to a thread (using something like a async based scoped thread library) it would fall over, right?</p>



<a name="212925799"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Internal%20%22self%22%20references%20into%20pinned%20types%20and%20Atomic/near/212925799" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Internal.20.22self.22.20references.20into.20pinned.20types.20and.20Atomic.html#212925799">(Oct 10 2020 at 17:16)</a>:</h4>
<p>Yes <a href="https://github.com/rust-lang/rust/issues/63818">https://github.com/rust-lang/rust/issues/63818</a></p>



<a name="212925906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Internal%20%22self%22%20references%20into%20pinned%20types%20and%20Atomic/near/212925906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Internal.20.22self.22.20references.20into.20pinned.20types.20and.20Atomic.html#212925906">(Oct 10 2020 at 17:18)</a>:</h4>
<p>Ok, then I will need to slightly change my API.</p>
<p>Luckily I can go from <code>&amp;mut</code> + internal referencing to <code>&amp;</code> + making nearly all parts using cell types. <span aria-label="stuck out tongue wink" class="emoji emoji-1f61c" role="img" title="stuck out tongue wink">:stuck_out_tongue_wink:</span>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>